package com.zhiyou100.controller.user;

import com.zhiyou100.dao.Impl.UserDaoImpl;
import com.zhiyou100.emtity.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-20 11:55
 **/
@WebServlet("/userUpdateEdit")
public class UserUpdateEditServlet extends HttpServlet {

    private UserDaoImpl dao = new UserDaoImpl();
    private User user;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        User user = new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        dao.upDate(user);
        request.getRequestDispatcher("/userQueryAll").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        user = dao.query(Integer.parseInt(request.getParameter("id")));
        request.getSession().setAttribute("user", user);
        request.getRequestDispatcher("./Adminastrator/user/editUser.jsp").forward(request, response);
    }
}
